<!DOCTYPE html>
<html>
<!--
Copyright 2009 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<head>
  <title>Google Earth API Samples - Polygon Styling</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  <!-- You should probably remove most of this extra cruft if you're copying
       this sample! -->
  <style type="text/css">@import "static/examples.css";</style>
  <style type="text/css">@import "static/prettify/prettify.css";</style>
  <script type="text/javascript" src="static/prettify/prettify.js"></script>
  
  <!-- Change the key below to your own Maps API key -->
  <script type="text/javascript" src="http://www.google.com/jsapi?hl=en&amp;key=ABQIAAAAwbkbZLyhsmTCWXbTcjbgbRSzHs7K5SvaUdm8ua-Xxy_-2dYwMxQMhnagaawTo7L1FE1-amhuQxIlXw"></script>
  <script type="text/javascript">
  /* <![CDATA[ */
  var ge;
  var polygonPlacemark;
  
  google.load("earth", "1");

  function init() {
    google.earth.createInstance('map3d', initCB, failureCB);
  }
  
  function initCB(instance) {
    ge = instance;
    ge.getWindow().setVisibility(true);
    
    // add a navigation control
    ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
    
    // add some layers
    ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
    ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);

    polygonPlacemark = ge.createPlacemark('');
    var polygon = ge.createPolygon('');
    polygonPlacemark.setGeometry(polygon);
    var outer = ge.createLinearRing('');
    polygon.setOuterBoundary(outer);
    
    // Square outer boundary.
    var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
    var coords = outer.getCoordinates();
    var lat = lookAt.getLatitude();
    var lon = lookAt.getLongitude();
    coords.pushLatLngAlt(lat - .05, lon - .05, 0); 
    coords.pushLatLngAlt(lat - .05, lon + .05, 0); 
    coords.pushLatLngAlt(lat + .05, lon + .05, 0); 
    coords.pushLatLngAlt(lat + .05, lon - .05, 0); 
    
    // Another square as the inner boundary.  Note that we can create
    // any number of inner boundaries.
    var innerBoundary = ge.createLinearRing('');
    polygon.getInnerBoundaries().appendChild(innerBoundary);
    coords = innerBoundary.getCoordinates();
    coords.pushLatLngAlt(lat - .02, lon - .02, 0); 
    coords.pushLatLngAlt(lat - .02, lon + .02, 0); 
    coords.pushLatLngAlt(lat + .02, lon + .02, 0); 
    coords.pushLatLngAlt(lat + .02, lon - .02, 0); 
    
    ge.getFeatures().appendChild(polygonPlacemark);
    
    // zoom in to the linestring we created
    lookAt.setRange(50000);
    ge.getView().setAbstractView(lookAt);
    
    
    document.getElementById('installed-plugin-version').innerHTML =
      ge.getPluginVersion().toString();
  }
  
  function failureCB(errorCode) {
  }
  
  function buttonClick() {
    // If polygonPlacemark doesn't already have a Style associated
    // with it, we create it now.
    if (!polygonPlacemark.getStyleSelector()) {
      polygonPlacemark.setStyleSelector(ge.createStyle(''));
    }
    
    // The Style of a Feature is retrieved as
    // feature.getStyleSelector().  The Style itself contains a
    // LineStyle and a PolyStyle, which are what we manipulate to change
    // the line color, line width, and inner color of the polygon.
    var lineStyle = polygonPlacemark.getStyleSelector().getLineStyle();
    lineStyle.setWidth(lineStyle.getWidth() + 2);
    // Color is specified in 'aabbggrr' format.
    lineStyle.getColor().set('66ff0000');
  
    // Color can also be specified by individual color components.
    var polyColor = 
      polygonPlacemark.getStyleSelector().getPolyStyle().getColor();
    polyColor.setA(200);
    polyColor.setB(0);
    polyColor.setG(255);
    polyColor.setR(255);
  }
  
  /* ]]> */
  </script>
</head>
<body onload="if(window.prettyPrint)prettyPrint();init();">
  <h1>Google Earth API Samples - Polygon Styling</h1>
  <dl>
            <dt>Last Modified:</dt><dd>06/08/2009</dd>
    <dt>Installed Plugin Version:</dt><dd id="installed-plugin-version">...</dd>
  </dl>
  <div style="clear: both;"></div>
  
  <div id="ui" style="position: relative;">
    <div id="map3d" style="border: 1px solid silver; width: 500px; height: 500px;"></div>

    <div id="extra-ui" style="position: absolute; left: 520px; top: 0;">
      <input type="button" onclick="buttonClick()" value="Stylize the Polygon!"/>
      
      <h2>Relevant Resources:</h2>
      <ul>
<li><a href="http://code.google.com/apis/earth/documentation/reference/interface_kml_polygon.html">KmlPolygon Reference</a></li>
<li><a href="http://code.google.com/apis/earth/documentation/reference/interface_kml_style.html">KmlStyle Reference</a></li>
<li><a href="http://code.google.com/apis/earth/documentation/reference/interface_kml_poly_style.html">KmlPolyStyle Reference</a></li>
<li><a href="http://code.google.com/apis/earth/documentation/reference/interface_kml_color.html">KmlColor Reference</a></li>
<a id="playground-button" href="http://code.google.com/apis/ajax/playground/?exp=earth#polygon_styling"><img src="static/playground-button.png"/></a>      </ul>
      <h2>Relevant Code Excerpt:</h2>      <pre class="prettyprint lang-js">// If polygonPlacemark doesn't already have a Style associated
// with it, we create it now.
if (!polygonPlacemark.getStyleSelector()) {
  polygonPlacemark.setStyleSelector(ge.createStyle(''));
}

// The Style of a Feature is retrieved as
// feature.getStyleSelector().  The Style itself contains a
// LineStyle and a PolyStyle, which are what we manipulate to change
// the line color, line width, and inner color of the polygon.
var lineStyle = polygonPlacemark.getStyleSelector().getLineStyle();
lineStyle.setWidth(lineStyle.getWidth() + 2);
// Color is specified in 'aabbggrr' format.
lineStyle.getColor().set('66ff0000');

// Color can also be specified by individual color components.
var polyColor = 
  polygonPlacemark.getStyleSelector().getPolyStyle().getColor();
polyColor.setA(200);
polyColor.setB(0);
polyColor.setG(255);
polyColor.setR(255);</pre>    </div>
  </div>
</body>
</html>
